package com.zxh.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.zxh.dao.ApplicantDao;
@WebServlet("/register.do")
public class ApplicantRegisterServlet extends HttpServlet{
		@Override
		protected void doGet(HttpServletRequest req, HttpServletResponse resp)
				throws ServletException, IOException {
			doPost(req, resp);		}
		
		@Override
		protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
			PrintWriter out = resp.getWriter();
			String email = req.getParameter("email");
			String password = req.getParameter("password");
			String code = req.getParameter("code");
			String usertype = req.getParameter("usertype");
			ApplicantDao dao = new ApplicantDao();
			boolean flag = dao.IsExitemail(email);
			//获取保存在session 中的验证码跟前端输入的验证码进行比较、
			//.equalsIgnoreCase 不区分字母大小写
			if(!((String) req.getSession().getAttribute("code")).equalsIgnoreCase(code) ){
				out.print("<script type ='text/javascript'>");
				out.print("alert('验证码错误，请重新输入');");
				out.print("window.location.href='register.jsp';");
				out.print("</script>");
			}else if(flag){
				//邮箱已经注册
				out.print("<script type ='text/javascript'>");
				out.print("alert('邮箱已被注册，请重新输入');");
				out.print("window.location.href='register.jsp';");
				out.print("</script>");
			}else{
				//保存注册信息
				dao.save(email,password,usertype);
				resp.sendRedirect("login.jsp");
			}
		}
}
